package com.aaa.inbound.service.impl;

import com.aaa.entity.base.Locator;
import com.aaa.entity.inbound.Receive;
import com.aaa.entity.inbound.Warehousing;
import com.aaa.inbound.feign.BaseFeign;
import com.aaa.inbound.mapper.ReceiveMapper;
import com.aaa.inbound.mapper.WarehousingMapper;
import com.aaa.inbound.service.WarehousingService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@Service
public class WarehousingServiceImpl extends ServiceImpl<WarehousingMapper, Warehousing> implements WarehousingService {

    @Autowired
    private BaseFeign baseFeign;

    @Autowired
    private WarehousingMapper warehousingMapper;

    @Autowired
    private ReceiveMapper receiveMapper;

    @Override
    public Page<Warehousing> searchWarehousing(Integer page, Integer limit, Warehousing warehousing) {
        Page<Warehousing> pageinfo = new Page<>(page, limit);
        String warehousingno = warehousing.getWarehousingno();
        String receiveno = warehousing.getReceiveno();
        String state = warehousing.getState();
        Long warehouseid = warehousing.getWarehouseid();
        QueryWrapper<Warehousing> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(warehousingno)) {
            queryWrapper.like("warehousingno", warehousingno);
        }
        if (StringUtils.isNotEmpty(receiveno)) {
            queryWrapper.like("receiveno", receiveno);
        }
        if (StringUtils.isNotEmpty(state)) {
            queryWrapper.like("state", state);
        }
        if (warehouseid != null) {
            queryWrapper.like("warehouseid", warehouseid);
        }
        this.baseMapper.selectPage(pageinfo, queryWrapper);

        return pageinfo;
    }

    @Override
    public R saveWarehousing(String no, Long locatorid) {
        Locator locator = baseFeign.selectLocById(locatorid);
        String sub = no.substring(0, 2);
        if (sub.equals("IR")) {
            QueryWrapper<Receive> rWrapper = new QueryWrapper<>();
            rWrapper.eq("receiveno", no);
            Receive receive = receiveMapper.selectOne(rWrapper);
            //判断单号是否存在
            if(receive == null){
                return R.error().code(500).message("单号不存在");
            }
            //判断接货单是否审核
            if (!receive.getState().equals("2")) {
                return R.error().code(500).message("接货单未审核，不能接货");
            }
            QueryWrapper<Warehousing> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("receiveno", no);
            Warehousing warehousingP = warehousingMapper.selectOne(queryWrapper);
            Warehousing warehousing = new Warehousing();
            //判断是否为首次入库
            if (warehousingP == null) {
                Date date = new Date();//获取当前的日期
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
                String str = df.format(date);//获取String类型的时间
                warehousing.setWarehousingno("IW" + str);
                warehousing.setLocatorid(locator.getId());
                warehousing.setLocatorcode(locator.getCode());
                warehousing.setLocatorname(locator.getName());
                warehousing.setReceiveno(no);
                warehousing.setWarehouseid(locator.getWarehouseid());
                warehousing.setState("0");
                String warehousingno = warehousing.getWarehousingno();
                warehousingMapper.insert(warehousing);
                return R.ok().code(200).message("入库单添加成功")
                        .data("warehousingno",warehousingno)
                        .data("receiveno",no);
            }
            //判断入库单已审核，不能重复接货
            if(warehousingP.getState().equals("2")){
                return R.error().code(500).message("入库单已审核，不能重复接货");
            }
            String warehousingno = warehousingP.getWarehousingno();
            return R.ok().code(201).message("非首次入库")
                    .data("warehousingno",warehousingno)
                    .data("receiveno",no);
        }
        if (sub.equals("IW")){
            QueryWrapper<Warehousing> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("warehousingno", no);
            Warehousing selectOne = warehousingMapper.selectOne(queryWrapper);
            //判断单号是否存在
            if(selectOne == null){
                return R.error().code(500).message("单号不存在");
            }
            //判断入库单已审核，不能重复接货
            if(selectOne.getState().equals("2")){
                return R.error().code(500).message("入库单已审核，不能重复接货");
            }
            //判断接货单是否审核
            QueryWrapper<Receive> rWrapper = new QueryWrapper<>();
            rWrapper.eq("receiveno", selectOne.getReceiveno());
            Receive receive = receiveMapper.selectOne(rWrapper);
            if (!receive.getState().equals("2")) {
                return R.error().code(500).message("接货单未审核，不能接货");
            }
            return R.ok().code(201).message("非首次入库")
                    .data("warehousingno",no)
                    .data("receiveno",selectOne.getReceiveno());
        }
        return R.error().code(500).message("订单号格式有误");
    }
}
